/*
 * SafetyCourseServiceImpl.java
 * Copyright 2018 Qunhe Tech, all rights reserved.
 * Qunhe PROPRIETARY/CONFIDENTIAL, any form of usage is subject to approval.
 */

package com.bearox.trans.service.impl;

import com.bearox.trans.data.domain.SafetyCourse;
import com.bearox.trans.data.dto.SafetyCourseDto;
import com.bearox.trans.mapper.SafetyCourseMapper;
import com.bearox.trans.service.SafetyCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Bearox
 */
@Service
public class SafetyCourseServiceImpl implements SafetyCourseService {
    private final SafetyCourseMapper safetyCourseMapper;

    public SafetyCourseServiceImpl(final SafetyCourseMapper safetyCourseMapper) {
        this.safetyCourseMapper = safetyCourseMapper;
    }

    @Override
    public int add(final SafetyCourseDto safetyCourseDto) {
        if (!checkSafetyCourse(safetyCourseDto)) {
            return 0;
        }
        final SafetyCourse safetyCourse = new SafetyCourse();
        BeanUtils.copyProperties(safetyCourseDto, safetyCourse);
        return safetyCourseMapper.add(safetyCourse);
    }

    @Override
    public List<SafetyCourseDto> getCourseList(final long start, final int num) {
        final List<SafetyCourse> safetyCourses = safetyCourseMapper.getCourseList(start, num);
        if(CollectionUtils.isEmpty(safetyCourses)) {
            return Collections.emptyList();
        }
        return safetyCourses.stream().map(a -> {
            final SafetyCourseDto safetyCourseDto = new SafetyCourseDto();
            BeanUtils.copyProperties(a, safetyCourseDto);
            return safetyCourseDto;
        }).collect(Collectors.toList());
    }

    private boolean checkSafetyCourse(final SafetyCourseDto safetyCourseDto) {
        return null != safetyCourseDto && safetyCourseDto.getCourseName() != null;
    }
}
